/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class code1 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums,0,nums.length-1);
    }

    public TreeNode dfs(int[] nums,int l,int r) {
        //
        if (l > r) {
            return null;
        }
        //平衡->以升序数组中间元素作为root
        int mid = l + (r-l)/2;
        TreeNode root = new TreeNode(nums[mid]);
        //构建左右子树
        root.left = dfs(nums,l,mid-1);
        root.right = dfs(nums,mid+1,r);

        return root;
    }
}